#include "CNavView.h"
#include <QObject>
#include "CNavModel.h"
#include "CNavDelegate.h"

CNavView::CNavView(QWidget  *parent)
	: QListView(parent)
{
	_navMode = new CNavModel();
	_navDelegate = new CNavDelegate();
	this->setModel(_navMode);
	this->setItemDelegate(_navDelegate);

	connect(this, SIGNAL(doubleClicked(const QModelIndex &)), _navMode, SLOT(Collapse(const QModelIndex &)));
	connect(this, SIGNAL(clicked(const QModelIndex&)), _navMode, SLOT(ModeItemClicked(const QModelIndex&)));
	connect(_navMode, SIGNAL(SignalClickedItem(QString)), this, SLOT(SlotClickedIndex(QString)));
	setStyleSheet(
		QString(
			"QListView{background-color:%1;"
			"border:0px solid %2;"
			"border-right-width:1px;}")
		.arg("239, 241, 250")
		.arg("214, 216, 224"));
}

CNavView::~CNavView()
{
}

void CNavView::setXmlFilePath(const QString& strPath)
{
	if (!strPath.isEmpty())
	{
		_navMode->ReadConfig(strPath);
		_navMode->Refresh();
	}
}

void CNavView::SetSelectedItem(const QString& strText)
{
	if (_navMode != nullptr)
	{
		int nCount = _navMode->rowCount();
		for (int i = 0; i < nCount; ++i)
		{
			QModelIndex Index = _navMode->index(i);
			QVariant var = _navMode->data(Index);
			QString strTmp = var.toString();
			if (strText.compare(strTmp) == 0)
			{
				this->setCurrentIndex(Index);
			}
		}
	}
}

void CNavView::SlotClickedIndex(QString strMenuID)
{
	Q_EMIT SignalClickedItem(strMenuID);
}
